package com.usf.jprovider.schema.provider.file;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlSeeAlso;

import com.usf.jprovider.object.JObjectNode;
import com.usf.jprovider.object.env.Environment;
import com.usf.jprovider.object.type.JType;
import com.usf.jprovider.schema.provider.AbstractProvider;
import com.usf.jprovider.schema.provider.file.formated.AbstractFormatedFileSource;

@XmlSeeAlso({
	TextFile.class,
	AbstractFormatedFileSource.class
})
public abstract class AbstractFileSource extends AbstractProvider {
	
	@XmlAttribute protected String path;
	
	protected AbstractFileSource() {
		super();
		path = "";
	}
	public String getPath() {
		return path;
	}

	@Override
	public final JType<?> create(Environment env, JObjectNode node) throws Exception {
		String value = createUnformated(env, node).toString();
		return type.toJType(value);
	}
	
	protected Object cacheValues(Environment env, JObjectNode node) throws Exception {
		String id = cacheIdentifier();
		Object value = env.getCache().get(id);
		if(value != null) return value;
		env.getCache().put(id, value = values(env));
		return value;
	}

	@Override
	public boolean isDependent(JObjectNode node) throws Exception {
		return false;
	}
	
	protected abstract String createUnformated(Environment env, JObjectNode node) throws Exception;
	protected abstract String cacheIdentifier();
	public abstract Object values(Environment env) throws Exception;
}
